package com.ychs.lawyer.service.impl;

import com.ychs.lawyer.bean.Contract;
import com.ychs.lawyer.bean.ContractCancellations;
import com.ychs.lawyer.bean.ResultCode;
import com.ychs.lawyer.mapper.ContractCancellationsMapper;
import com.ychs.lawyer.mapper.ContractMapper;
import com.ychs.lawyer.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/07/30/10:33
 * @Description:
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ContractCancellationsMapper contractCancellationsMapper;
    @Override
    public List<Contract> getContractsByCondition(Contract contract) {
        return contractMapper.getContractsByCondition(contract);
    }

    @Override
    public int addContract(Contract contract) {
        return contractMapper.addContract(contract);
    }

    @Override
    public int editContract(Contract contract) {
        return contractMapper.editContract(contract);
    }

    @Override
    public int changeContractActive(Contract contract) {
        return contractMapper.changeContractActive(contract);
    }

    @Transactional
    @Override
    public int delContract(Contract contract) {
//        修改状态

//        添加注销表里
        ContractCancellations contractCancellations = new ContractCancellations();
        contractCancellations.setCancellationId(contract.getContractId());
        contractCancellations.setContractName(contract.getContractName());
        contractCancellations.setContractType(contract.getContractType());
        contractCancellations.setLawyerName(contract.getLawyerName());
        contractCancellations.setCancellationReason(contract.getCancellationReason());
        contractCancellations.setOperator(contract.getOperator());
        contractCancellations.setRemarks(contract.getRemarks());

        int res2 = contractCancellationsMapper.addContractCancellation(contractCancellations);
        if(res2>0) {
             return contractMapper.changeContractActive(contract);
        }
        else{
           return 0;
        }



    }

    @Override
    public List<ContractCancellations> getCancelContract(ContractCancellations contractCancellations) {
             return contractCancellationsMapper.getCancelContract(contractCancellations);
    }

    @Transactional
    @Override
    public int reStart(String contractName) {
        int i = contractCancellationsMapper.delCancelContract(contractName);
        return contractCancellationsMapper.reStart(contractName);
    }
}
